﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UIControllTool;

namespace ModuleQT {
    [ToolCreateFormAttribute]
    public class MainFormConfig : InitUserControl, InitUserControlInterface {
        public static string ButtonActiveName;

        public string GetButtonActiveName() {
            return ButtonActiveName;
        }
        public void SetButtonActiveName(string value) {
            // 查找 MainForm 中名为 "FunctionListBar" 的 Panel

            AntdUI.Panel FunctionBarListPanel = Tool.GetControlByForm<AntdUI.Panel>(Form, "FunctionBarListPanel");
            AntdUI.Panel FunctionBody = Tool.GetControlByForm<AntdUI.Panel>(Form, "FunctionBody");
            AntdUI.Button ButtonActive = Tool.GetControlByForm<AntdUI.Button>(Form, value);
            FunctionBody.Controls.Clear();
            foreach (Control control in FunctionBarListPanel.Controls) {
                if (control is AntdUI.Button) {
                    AntdUI.Button AllButtonOnce = (AntdUI.Button)control;
                    AllButtonOnce.DefaultBack = Tool.GetColorBy16("FFFFFF");
                }
            }
            ButtonActive.DefaultBack = Tool.GetColorBy16("F0F0F0");

            InitUserControl initUserControl = Tool.Functions[value] as InitUserControl;
            FunctionBody.Dock = DockStyle.Fill;
            FunctionBody.Controls.Add(initUserControl.userControl);

            ButtonActiveName = value;
        }
        public MainFormConfig(Form form) {
            Form = form;
            FormName = "FormWin";
        }
        public void InitUserControlImpl() {
            AntdUI.Panel FunctionBarListPanel = Tool.GetControlByForm<AntdUI.Panel>(Form, "FunctionBarListPanel");
            AntdUI.Panel FunctionBody = Tool.GetControlByForm<AntdUI.Panel>(Form, "FunctionBody");
            int index = 0;
            InitUserControl defaultFunction=null;
            foreach (string item in Tool.Functions.Keys) {
                if (index == 0) {
                    defaultFunction = Tool.Functions[item] as InitUserControl;
                    index++;
                }
                InitUserControl initUserControl = Tool.Functions[item] as InitUserControl;
                if (ButtonActiveName!=null&& ButtonActiveName.Equals(initUserControl.FunctionName)) {
                    FunctionBarListPanel.Controls.Add(MyMyButtonByMainFromBar(item,true));
                    if (initUserControl.FunctionName.Equals(ButtonActiveName)) {
                        FunctionBody.Controls.Add(initUserControl.userControl);
                        //test
                        AntdUI.Panel FunctionListPanel = Tool.GetControlByForm<AntdUI.Panel>(initUserControl.userControl, "FunctionListPanel");
                        FunctionListPanel.Dock = DockStyle.Fill;
                        Form.Controls.Add(FunctionBarListPanel);
                    }
                } else {
                    FunctionBarListPanel.Controls.Add(MyMyButtonByMainFromBar(initUserControl.FunctionName));
                }
            }
            IntiControlData();
            if (ButtonActiveName == null) {
                SetButtonActiveName(defaultFunction.FunctionName);
            }
        }

        public AntdUI.Button MyMyButtonByMainFromBar(string Text, bool IsMain = false) {
            AntdUI.Button MyButton = new AntdUI.Button();
            MyButton.Dock = DockStyle.Left;
            MyButton.AutoSize = true;
            if (IsMain) {
                MyButton.DefaultBack = Tool.GetColorBy16("FFFFFF");
            } else {
                MyButton.DefaultBack = Tool.GetColorBy16("F0F0F0");
            }

            MyButton.Text = Text;
            MyButton.Name = Text;
            MyButton.Radius = 0;
            MyButton.Click += MyButton_Click;
            return MyButton;
        }

        private void MyButton_Click(object sender, EventArgs e) {
            AntdUI.Button button = (AntdUI.Button)sender;
            SetButtonActiveName(button.Text);
        }

        public void SaveUserControlImpl() {
            SavePropertiesToParentHashTable();
        }
    }
}
